<?php
Yii::import('common.models.page.*');
class BusinessAdsController extends Controller{
    public $activeMenu = 'cms';
    public function actionCreate(){

        $model = new BusinessAds;
        if(isset($_POST['BusinessAds'])){

            $model->attributes = $_POST['BusinessAds'];

            //check photo

            if(($imageUploadFile = CUploadedFile::getInstance($model, 'photo'))){ // only do if file is really uploaded

                $imageFileName = time().$imageUploadFile->name;

                $imageUploadFile->saveAs(UPLOAD_PATH . $imageFileName);

                //delete previous image

                if($model->image != '')
                    @unlink(UPLOAD_PATH.$model->image);

                $model->image = $imageFileName;

            }

            if($model->save()){
                Yii::app()->user->setFlash('success', 'The ads has been created');
                //redirect to update
                $this->redirect(array('update', 'id' => $model->id));

            }else{
                Yii::app()->user->setFlash('error', 'Sorry, please try again');
            }

        }

        $this->render('create', array(

            'model' => $model

        ));

    }



    public function actionUpdate($id){

        $model = $this->_loadModel($id);



        if(isset($_POST['BusinessAds'])){

            $model->attributes = $_POST['BusinessAds'];

            $model->isNewRecord = FALSE;



            //check photo

            if(($imageUploadFile = CUploadedFile::getInstance($model, 'photo'))){ // only do if file is really uploaded

                $imageFileName = time().$imageUploadFile->name;

                $imageUploadFile->saveAs(UPLOAD_PATH . $imageFileName);

                //delete previous image
                if($model->image != '')
                    @unlink(UPLOAD_PATH.$model->image);
                $model->image = $imageFileName;
            }



            if($model->save()){
                Yii::app()->user->setFlash('success', 'The ads has been updated');
            }else{
                Yii::app()->user->setFlash('error', 'Sorry, please try again');
            }
        }

        $this->render('update', array(
            'model' => $model
        ));
    }

    /**
     * delete business ads
     * @param type $id
     */

    public function actionDelete($id){
        if(!Yii::app()->request->isPostRequest)
            throw new CHttpException(404, 'The page you has been requested not exists');

        $model = BusinessAds::model()->findByPk($id);
        $model->isNewRecord = FALSE;

        if(!$model){
            throw new CHttpException(404, 'The page you has been requested not exists');
        }

        if($model->image != '')
                @unlink(UPLOAD_PATH.$model->image);

        $model->delete();

    }

    public function actionIndex(){
        $model = new BusinessAds('search');
        if(isset($_GET['BusinessAds']))
            $model->attributes = $_GET['BusinessAds'];

        $this->render('index', array(
            'model' => $model
        ));

    }

    private function _loadModel($id){
        $model = BusinessAds::model()->findByPk($id);
        if(!$model)
            throw new CHttpException(404, 'The page you has been requested not exists');
        return $model;
    }
}
